package kingwing.dc.core.sevice;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import kingwing.dc.core.model.Infos;
import kingwing.dc.core.model.Rule;
import kingwing.dc.core.model.RuleDetail;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

/**
 * 单页面采集（静态采集）
 * @author Kevin
 * 2017年10月27日
 */
public class SinglePageHtmlProcessor implements PageProcessor{
	
	
	 /**
     * 网站配置类
     */
    private Site site;
    
    /*
     * 采集规则
     */
    private Rule rule;
    
    /**
     * 采集规则明细-采集字段
     */
    private List<RuleDetail> ruleDetails;
    
    public SinglePageHtmlProcessor(Site site, Rule rule, List<RuleDetail> ruleDetails) {
		super();
		this.site = site;
		this.rule = rule;
		this.ruleDetails = ruleDetails;
	}
	

	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		
		String title = "";
		String mainBody = "";
		String time = "";
		String URL = "";
		String listTitle = "";
		
		for (RuleDetail ruleDetail : ruleDetails) {
			switch (ruleDetail.getFieldType()) {
			case 0:
				title = ruleDetail.getRuleDetailSelector();
				break;
			case 1:
				mainBody = ruleDetail.getRuleDetailSelector();
				break;
			case 2:
				time = ruleDetail.getRuleDetailSelector();
				break;
			case 3:
				URL = ruleDetail.getRuleDetailSelector();
				break;
			case 4:
				listTitle = ruleDetail.getRuleDetailSelector();
				break;
			}
		}
		
		//正则判断传入的URL是列表页还是内容页
		if (page.getUrl().regex(rule.getHelperUrlRegexes()).match()) {
			List<Selectable> nodes = page.getHtml().xpath(listTitle).nodes();
			List<Infos> infosList = new ArrayList<Infos>();
			for (Selectable selectable : nodes) {
				
				Infos infos = new Infos();
				
				boolean flag = false;	//判断字段获取的值是否为空
				
				infos.setRuleName(rule.getRuleName());
				String field1 = selectable.xpath(title).get();
				if (StringUtils.isNotBlank(field1)) {
					infos.setField1(field1.trim());
				}else {
					flag = true;
				}
				String field3 = selectable.xpath(time).get();
				if (StringUtils.isNotBlank(field3)) {
					infos.setField3(this.formatToDate(field3.trim()));
				}else {
				}
				
				String field4 = selectable.xpath(URL).links().get();
				if (StringUtils.isNotBlank(field4)) {
					infos.setField4(field4.trim());
				}else {
					flag = true;
				}
				
				//如果存在空字段，则不进行存储
				if (!flag) {
					infosList.add(infos);
				}
			}
			page.putField("infos", infosList);
		} else {
			String mainText = page.getHtml().xpath(mainBody).toString();
			page.putField("mainText", mainText);
		}
		
	}

	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}
	
	/**
	 * 返回一个格式良好的日期字符串
	 * @param field3
	 * @return
	 */
	private String formatToDate(String field3) {
		String reg = "([0-9]{4})[-/\\.年]?([0-1]?[0-9]{1})[-/\\.月]?([0-3]?[0-9]{1})[日]?";//日期正则表达式
		Pattern pattern = Pattern.compile (reg);
		Matcher matcher = pattern.matcher (field3);//使用正则表达式判断日期
		if (matcher.find ()) {
			return matcher.group ();
		}else {
			return field3;
		}
	}

}
